#include "Instruction.hpp"
#include <stdlib.h>

using namespace std;

Instruction::Instruction() {
}

Instruction::Instruction(const Instruction& inst) {
}

Instruction::~Instruction() {
}

Instruction::Instruction(string Etiquette, string operateur)
{
    this->Operateur = operateur;
    if(this->Operateur.compare("DEB") == 0)
    {
        this->Ligne3adresses =
			Etiquette + " : " + "\t" + this->CodeInstr->Deb();
    }

}

Instruction::Instruction(string operateur, Operande &Resultat,
		Operande &Arg1, Operande &Arg2, TableId &TI)
{
    this->Res = &Resultat;
    this->Operan1 = &Arg1;
    this->Operan2 = &Arg2;
    this->Operateur = operateur;


    if(this->Operateur.compare("ADD") == 0)
    {
            this->Ligne3adresses =
				"\t" + this->CodeInstr->Add(TI, *(this->Res),
						*(this->Operan1), *(this->Operan2));
    }
    else if(this->Operateur.compare("SUB") == 0)
    {
            this->Ligne3adresses =
				"\t" + this->CodeInstr->Sub(TI, *(this->Res),
						*(this->Operan1), *(this->Operan2));
    }
	/*
    else if(this->Operateur.compare("CPY") == 0)
    {
            this->Ligne3adresses =
				"\t" + this->CodeInstr->Cpy(TI, *(this->Res),
						*(this->Operan1));
    }
	*/
    else if(this->Operateur.compare("MUL") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Mul(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
    else if(this->Operateur.compare("DIV") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Div(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
    else if(this->Operateur.compare("MOD") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Mod(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
    else if(this->Operateur.compare("AND") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->And(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
	/*
    else if(this->Operateur.compare("IF") == 0)
    {
        this->Ligne3adresses =
        	"\t" + this->CodeInstr->If(TI, *(this->Res));
    }
	*/
    else if(this->Operateur.compare("OR") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Or(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
    else if(this->Operateur.compare("XOR") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Xor(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
	/*
    else if(this->Operateur.compare("NOT") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Not(TI, *(this->Res),
					*(this->Operan1));
    }
	*/
    else if(this->Operateur.compare("EQ") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Eq(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
    else if(this->Operateur.compare("NEQ") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Neq(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
    else if(this->Operateur.compare("LT") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Lt(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
    else if(this->Operateur.compare("LTE") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Lte(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
    else if(this->Operateur.compare("GT") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Gt(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }
    else if(this->Operateur.compare("GTE") == 0)
    {
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Gte(TI, *(this->Res),
					*(this->Operan1), *(this->Operan2));
    }

}

Instruction::Instruction(string operateur, Operande &Resultat,
		Operande &Arg1, TableId &TI)
{
    this->Res = &Resultat;
    this->Operan1 = &Arg1;
    this->Operateur = operateur;

    if(this->Operateur.compare("NOT") == 0)
    {
		/*
        this->Ligne3adresses =
			"\t" + this->CodeInstr->Not(TI, *(this->Res),
					*(this->Operan1));
		*/
    }
	else if(this->Operateur.compare("IF") == 0)
    {
		/*
        this->Ligne3adresses =
        	"\t" + this->CodeInstr->If(TI, *(this->Res));
		*/
    }
	else if(this->Operateur.compare("CPY") == 0)
    {
        this->Ligne3adresses = "\t";
		this->Ligne3adresses += this->CodeInstr->Cpy(TI, *(this->Res),
					*(this->Operan1));
    }
	else {
		cout << "ERROR : UNKNOWN INSTRUCTION OPERATOR : "
			<< operateur << "." << endl;
		exit(44);
	}
}


string Instruction::getaffichageLigne()
{
    return this->Ligne3adresses;
}

string Instruction::getOperateur()
{
    //if(Operateur != "")
        return Operateur;
}


